/**
 * fileName: MR_YLES_Delete_file_Timed
 * @NApiVersion 2.1
 * @NScriptType MapReduceScript
 */

import {EntryPoints} from 'N/types'
import search = require('N/search')
import record = require('N/record')
import log = require('N/log')
import file = require('N/file')
import {
    getScriptParams
} from "../tools/YLES_Tool";
import {
    getCsvParentFolder
} from "../tools/YLES_SEARCH_TOOL";

export function getInputData(cxt: EntryPoints.MapReduce.getInputDataContext) {
    try{
        let days = getScriptParams('custscript_Save_Import_CSV_Day')as number;
        let now = new Date();
        let arr =[];
        for(let i=0;i < days;i++){
            let date = new Date(now.getTime() - i * 24 * 60 * 60 * 1000);
            let year = date.getFullYear();
            let mouth = date.getMonth() + 1;
            let day = date.getDate();
            let name = "ibCsv_" + year + "-" + mouth + "-" + day;
            log.debug("name",name);

            arr.push(name);
        }

        let foldid = getCsvParentFolder();
        //搜索yles_csv_fold下的文件夹
        let filters = [["parent","anyof",foldid]];
        let fiter: any=[];
        if(arr.length>0){
            // @ts-ignore
            filters.push("AND");
            for(let l=0;l<arr.length;l++){
                if(l!=0){
                    fiter.push("AND");
                }
                fiter.push(["name","isnot",arr[l]]);
            }
        }
        if(fiter.length>0){filters.push(fiter) }
        let columns=[search.createColumn({name: "internalid", label: "内部 ID"}),
            search.createColumn({name: "name", sort: search.Sort.ASC, label: "名称"})];
        let data=[];//存放要删除的文件夹的内部id
        let folderSearchObj = search.create({
            type: "folder",
            filters: filters,
            columns:columns
        });
        let rec = folderSearchObj.run().getRange({start:0,end:1000});
        if(rec.length>0){
            for(let li=0;li<rec.length;li++){
                data.push(rec[li].getValue("internalid"));
            }
        }
        return data;
    }catch (e) {
        log.debug("get.e",e)
    }

}


export function reduce(context: EntryPoints.MapReduce.reduceContext)  {
    try{
        // @ts-ignore
        let value = JSON.parse(context.values);
        log.debug("value",value);

        let folderSearchObj = search.create({
            type: "folder",
            filters:
                [
                    ["internalid", "anyof", value]
                ],
            columns:
                [
                    search.createColumn({name: "name", join: "file", label: "名称"}),
                    search.createColumn({name: "internalid", join: "file", label: "内部 ID"}),
                    search.createColumn({name: "name", sort: search.Sort.ASC, label: "名称"})
                ]
        });
        let rec = folderSearchObj.run().getRange({start: 0, end: 1000});

    if (rec.length > 0) {
        for (let i = 0; i < rec.length; i++) {
            let fileid: any= rec[i].getValue({name: "internalid", join: "file"});
            file.delete({id:fileid});
        }
    }
    record.delete({type:"folder",id:value});
    }catch (e) {
        log.debug("map.e",e);
    }
}

export function summarize(summary: EntryPoints.MapReduce.summarizeContext) {
    log.debug("执行完成"," ");
}
